package com.xiaoshuidi.cloud.module.system.controller.admin.ocr;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.module.system.controller.admin.ocr.vo.CompanyOcrVO;
import com.xiaoshuidi.cloud.module.system.convert.ocr.OcrConvert;
import com.xiaoshuidi.cloud.module.system.service.ocr.OcrService;
import com.xiaoshuidi.cloud.module.system.service.ocr.dto.OcrDataDTO;
import com.xiaoshuidi.cloud.module.system.service.oos.OosService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.Map;
import java.util.Objects;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.error;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.module.system.enums.ErrorCodeConstants.OCR_RECOGNITION_FAILED;

@Tag(name =  "管理后台 - OCR识别")
@RestController
@RequestMapping("/system/ocr")
public class OcrController {

    @Resource
    private OcrService ocrService;

    @Resource
    private OosService oosService;

    @PostMapping("/business-license-file")
    @PermitAll
    @Operation(summary = "ocr识别营业执照文件")
    @OperateLog(enable = false)
    public CommonResult<CompanyOcrVO> ocrBusinessLicense(@RequestParam("file") MultipartFile file) {
        Map<String, Object> stringObjectMap = oosService.uploadFile(file, file.getOriginalFilename());
        if (Objects.nonNull(stringObjectMap) && !stringObjectMap.isEmpty()){
            String url = Objects.nonNull(stringObjectMap.get("pathUrl"))?(String)stringObjectMap.get("pathUrl"):"";
            if (StringUtils.isNotBlank(url)){
                OcrDataDTO ocrData = ocrService.businessLicense(null, url);
                if (Objects.nonNull(ocrData)){
                    CompanyOcrVO convert = OcrConvert.INSTANCE.convert(ocrData);
                    convert.setBusinessLicencePic(url);
                    return success(convert);
                }
            }
        }
        return error(OCR_RECOGNITION_FAILED);
    }

    @PostMapping("/business-license-url")
    @PermitAll
    @Operation(summary = "ocr识别营业执照url")
    @OperateLog(enable = false)
    public CommonResult<CompanyOcrVO> ocrBusinessLicenseUrl(@RequestParam("url") String url) throws Exception {
        OcrDataDTO ocrData = ocrService.businessLicense(null, url);
        if (Objects.nonNull(ocrData)){
            CompanyOcrVO convert = OcrConvert.INSTANCE.convert(ocrData);
            convert.setBusinessLicencePic(url);
            return success(convert);
        }
        return error(OCR_RECOGNITION_FAILED);
    }

}